package alps.common.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by alps on 2016/12/6.
 */
public abstract class CommonRecyclerViewAdapter<T, V extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<V> {

    private List<T> mDatas = new ArrayList<>();

    private Context mContext;

    public LayoutInflater inflater;

    private OnCommonItemClickListener<T> mListener;

    public CommonRecyclerViewAdapter(Context context) {
        this. mContext = context;
        inflater = LayoutInflater. from(mContext);

    }

    public void setDatas(List<T> datas) {
        if (datas != null) {
            mDatas = datas;
            notifyDataSetChanged();
        }
    }

    public void setOnItemClickListener(OnCommonItemClickListener<T> listener) {
        mListener = listener;
    }

    public final T getData(int position) {
        if (position >= 0 && mDatas != null && mDatas.size() > position) {
            return mDatas.get(position);
        }
        return  null;
    }

    protected void onItemClicked(T data, int position) {
        if (mListener != null) {
            mListener.onItemClick(data, position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public interface OnCommonItemClickListener<T> {
        void onItemClick(T data, int position);

        void onItemLongClicked(T data, int position);
    }

}